package edu.gxmzu.task.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.gxmzu.task.entity.Message;
import edu.gxmzu.task.entity.TaskDailyCount;
import edu.gxmzu.task.entity.TaskDistributeEntity;
import edu.gxmzu.task.entity.WorkStatusCount;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


/**
 * author: lrf
 * time: 2024-11-29
 * version : V1
 */

@Mapper
public interface TaskDisposeMapper extends BaseMapper<TaskDistributeEntity> {

    /**
     * 获取下发人员接口
     * @return
     */
    @Select("SELECT u.user_id, u.name, a.username,u.role_id FROM user_info AS u INNER JOIN accounts AS a ON a.user_id = u.user_id WHERE role_id = 0 and u.name LIKE CONCAT('%', COALESCE(#{name}, ''), '%') and a.deletion_state=0")
    List<Map<String, Object>> getUserName(String name);

    /**
     * 获取附件上传ID
     * @return
     */
    @Select("select id from attachments order by id desc limit 1")
    String getAttachmentsId();

    /**
     * 已办代办完成任务
     * @param
     * @return
     */
    @Select("SELECT " +
            "    COUNT(CASE WHEN work_status_id = 0 THEN 1 END) AS count_status_0, \n" +
            "    COUNT(CASE WHEN work_status_id = 1 THEN 1 END) AS count_status_1, \n" +
            "    COUNT(CASE WHEN work_status_id = 2 THEN 1 END) AS count_status_2 \n" +
            "FROM works " +
            "WHERE work_status_id IN (0, 1, 2) " +
            "AND (IFNULL(#{userid}, '') = '' OR appointor_id = #{userid})")
    WorkStatusCount countWorkStatus(String userid);

    /**
     * 全部任务
     * @param
     * @return
     */
    @Select("SELECT count(*) AS tasks FROM works where user_id = #{userid} or appointor_id = #{userid}")
    int tasksList(String userid);

    /**
     * 已到期任务
     * @param
     * @return
     */
    @Select("select count(*) from works where user_id =#{userid} and end_time <= #{nweDate}")
    int tasksGuoQiList(@Param("userid")String userid,  @Param("nweDate")Long newDate);
}
